package leetcode.code0335;

public class Solution1 extends Solution335 {

	@Override
	public boolean isSelfCrossing(int[] d) {
		int n = d.length;
		if (n < 4)
			return false;
		for (int i = 3; i < n; i++) {
			if (d[i] >= d[i - 2] && d[i - 1] <= d[i - 3])
				return true;
			if (i >= 4 && d[i - 1] == d[i - 3] && d[i] + d[i - 4] >= d[i - 2])
				return true;
			if (i >= 5 && d[i - 1] <= d[i - 3] && d[i - 2] > d[i - 4] && d[i] + d[i - 4] >= d[i - 2]
					&& d[i - 1] + d[i - 5] >= d[i - 3])
				return true;
		}
		return false;
	}


	private int len;

	public boolean isSelfCrossing1(int[] distance) {
		this.len = distance.length;
		if (len < 4)// 不足4个撞不上
			return false;
		int index = 3;
		if (index < len) {
			if (distance[index - 1] <= distance[index - 3] && distance[index] >= distance[index - 2])
				return true;
		}
		index = 4;
		if (index < len) {

		}
		return false;// 向东
	}

	private boolean f(int[] distance, int index) {
		if (index == len) {
			return false;
		}
		if (this.check(distance, index)) {
			return true;
		}
		return this.f(distance, index + 1);
	}

	private boolean check(int[] distance, int index) {
		if (distance[index - 1] <= distance[index - 3] && distance[index] >= distance[index - 2]) {
			return true;
		}
		if (index - 4 >= 0 && distance[index - 1] == distance[index - 3]
				&& distance[index - 2] == distance[index] + distance[index - 4]) {// 转一圈成环了
			return true;
		}
		if (index - 5 >= 0 && distance[index - 3] == distance[index - 1] + distance[index - 5]
				&& distance[index - 2] == distance[index] + distance[index - 4]) {// 转一圈成环了
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
		so.debug6();
		so.debug7();
		so.debug8();
	}

}
